package com.polaris.live.resp.bean.decoration

/**
 * UserDecorationChatBubbleExtBean 聊天气泡数据
 * @author Created by 天晴 on 2024/1/31 10:31
 * @since 1.0.0
 **/
data class UserDecorationChatBubbleExtBean(
    /**
     * 地址
     *
     * 这个通常最后需要添加left和right才是完整的地址(在.9.png之前)
     */
    override val url: String,
    /**
     * 宽度
     */
    override val width: Int,
    /**
     * 高度
     */
    override val height: Int,
    /**
     * 左气泡后缀
     */
    val leftBubble: String,
    /**
     * 右气泡后缀
     */
    val rightBubble: String,
    /**
     * 左边距
     */
    val left: Int,
    /**
     * 上边距
     */
    val top: Int,
    /**
     * 右边距
     */
    val right: Int,
    /**
     * 下边距
     */
    val bottom: Int,
    /**
     * 左文字颜色
     */
    val leftColor: String,
    /**
     * 右文字颜色
     */
    val rightColor: String,
    /**
     * 翻译图标
     */
    val translationUrl: String?,
    /**
     * 翻译线条颜色
     */
    val lineColor: String
) : AbstractDecorationExtBean() {

    private var leftUrl: String? = null
    private var rightUrl: String? = null

    fun getLeftUrl(): String {
        return leftUrl ?: getSlicing(leftBubble).apply {
            leftUrl = this
        }
    }

    fun getRightUrl(): String {
        return rightUrl ?: getSlicing(rightBubble).apply {
            rightUrl = this
        }
    }

    private fun getSlicing(direction: String): String {
        if (url.isEmpty()) {
            return ""
        }
        val index = url.lastIndexOf("/")
        if (index == -1) {
            return ""
        }
        val fileBefore = url.substring(0, index)
        val fileAfter = url.substring(index + 1)

        val dotIndex = fileAfter.indexOf(".")
        if (dotIndex == -1) {
            return ""
        }
        val fileName = fileAfter.substring(0, dotIndex)
        val suffix = fileAfter.substring(dotIndex + 1)
        return "$fileBefore/$fileName$direction.$suffix"
    }
}